
import 'package:baselib/utils/color_base_util.dart';
import 'package:baselib/utils/drawable_base_util.dart';
import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:baselib/utils/string_base_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'inkwell_button.dart';

/**
 * 普通控制输入框
 */
class SingleInputControlWidget extends StatelessWidget {
  String title;
  TextStyle titleStyle;
  EdgeInsetsGeometry titleMargin;
  bool isShowTitleRightImg;
  double titleRightImgWidth;
  Function titleRightImgClick;

  Function onSubmitted;
  Function onChanged;
  List<TextInputFormatter> inputFormatters;
  double width;
  double height;
  bool readOnly;
  TextInputType keyboardType;
  TextInputAction textInputAction;
  EdgeInsetsGeometry contentPadding;
  String hintText;
  Color borderColor;
  double borderRadius;
  BoxDecoration decoration;
  int maxLength;
  int maxLines;
  int minLines;
  Color underLineFocusedBorderColor;
  Color underLineDefaultBorderColor;
  bool isNecessary;
  TextAlign editTextAlign;
  TextStyle contTextStyle;
  bool autofocus;
  TextEditingController textEditingController;

  SingleInputControlWidget({
    Key key,
    this.title,
    this.titleRightImgWidth,
    this.titleRightImgClick,
    this.isShowTitleRightImg:false,
    this.titleStyle,this.titleMargin,
    this.onSubmitted,this.onChanged,this.inputFormatters,this.width,this.height,
    this.readOnly,this.keyboardType,this.textInputAction,this.contentPadding,this.hintText,this.borderColor,
    this.borderRadius,this.decoration,this.maxLength,this.maxLines,this.minLines,
    this.underLineFocusedBorderColor,this.underLineDefaultBorderColor,this.isNecessary:true,
    this.editTextAlign,this.contTextStyle,this.autofocus,this.textEditingController,
  }):super(key: key);
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        (title??'').isEmpty?Container():Container(
          child: Row(
            children: [
              (isNecessary??false)?Text(
                '*',
                style: titleStyle??TextStyle(
                    fontSize: PxBaseUtil.instance.px_14,
                    color: ColorBaseUtil.instance.color_red
                ),
              ):Container(),
              Expanded(child: Text(
                '${title}',
                style: titleStyle??TextStyle(
                    fontSize: PxBaseUtil.instance.px_14,
                    color: ColorBaseUtil.instance.color_383838
                ),
              ),),
              (!(isShowTitleRightImg??false))?Container():
              InkWellButton(
                child: ImagesUtil.loadAssetImage(
                    DrawableBaseUtil.instance.icon_dustbin,
                    width: titleRightImgWidth??PxBaseUtil.instance.px_16
                ),
                onPressed: (){
                  if(titleRightImgClick!=null)titleRightImgClick();
                },
              )
            ],
          ),
          margin: titleMargin??EdgeInsets.only(
              bottom: PxBaseUtil.instance.px_5
          ),
        ),
        Container(
          width: width??PxBaseUtil.instance.screenWidth,
          margin: EdgeInsets.all(PxBaseUtil.instance.px_0),
          decoration: decoration??BoxDecoration(),
          alignment: Alignment.topLeft,
          child: TextFormField(
            style: contTextStyle??TextStyle(
                fontSize: PxBaseUtil.instance.px_14,
                color: ColorBaseUtil.instance.color_383838
            ),
            // initialValue: defaultCont,
            enabled:true ,
            textAlign: editTextAlign??TextAlign.start,
            maxLength: maxLength??50,
            maxLines: maxLines??1,
            minLines: minLines??1,
            obscureText: false,
            autofocus:autofocus?? false,
            controller: textEditingController,
            readOnly: readOnly??false,
            textInputAction: textInputAction??TextInputAction.done,
            keyboardType: keyboardType??TextInputType.text,
            onFieldSubmitted: (val){
              if(onSubmitted!=null)onSubmitted(val);
            },
            onChanged: (value) {
              if(onChanged!=null)onChanged(value);
            },
            cursorColor: ColorBaseUtil.instance.color_black,
//          // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
            inputFormatters:inputFormatters,
            decoration: InputDecoration(
                contentPadding: contentPadding??EdgeInsets.only(
                  left: PxBaseUtil.instance.px_0,
                  right: PxBaseUtil.instance.px_2,
                  top: PxBaseUtil.instance.px_2,
                  bottom: PxBaseUtil.instance.px_5,
                ),
                isDense: true,
                hintText: hintText??'${StringBaseUtil.instance.pleaseinput}',
                hintStyle: TextStyle(
                    fontSize: PxBaseUtil.instance.px_14,
                    color: ColorBaseUtil.instance.color_dedede
                ),
                counterText: "",
                focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                        color: underLineFocusedBorderColor??ColorBaseUtil.instance.color_blue,
                        width: PxBaseUtil.instance.px_0d5
                    )
                ),
                enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                        color: underLineDefaultBorderColor??ColorBaseUtil.instance.color_d4d4d4,
                        width: PxBaseUtil.instance.px_0d5
                    )
                )
            ),
          ),
        )
      ],
    );
  }
}
